package com.springcloud.zuul.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author
 * @create 2020-11-12 19:07
 */
@Configuration
public class CrossOriginConfiguration {

    @Bean
    public CorsFilter corsFilter(){
        //跨域配置源
        UrlBasedCorsConfigurationSource source =
                new UrlBasedCorsConfigurationSource();
        //跨域配置类
        CorsConfiguration configuration =
                new CorsConfiguration();
        //允许所有的请求头通过
        configuration.addAllowedHeader("*");
        //允许所有的服务器请求
        configuration.addAllowedOrigin("*");
        //允许所有的请求方法
        configuration.addAllowedMethod("*");
        //将配置添加到配置源
        source.registerCorsConfiguration("/**",configuration);
        return new CorsFilter(source);
    }
}
